package com.william.jdbcplus.core.dialect.ddl.fk.impl;

import com.william.jdbcplus.common.entity.Entity;
import com.william.jdbcplus.core.dialect.ddl.fk.IFk;
import com.william.jdbcplus.core.meta.TableFkInfo;
import com.william.jdbcplus.core.meta.TableHelperUtil;
import com.william.jdbcplus.core.meta.TableInfo;
import com.william.jdbcplus.core.toolkit.StringPool;
import com.william.jdbcplus.core.util.datameta.TableMetaFkInfo;

/**
 * SqlServer 外键实现
 *
 * @author william
 */
public class SqlServerFk implements IFk {
    /**
     * 新增外键
     *
     * @param clazz
     * @param fkInfo
     * @return
     */
    @Override
    public <M extends Entity> String add(Class<M> clazz, TableFkInfo fkInfo) {
        //alter table 从表 add constraint 外键名（形如：FK_从表_主表） foreign key (从表外键字段) references 主表(主键字段);
        StringBuffer sqlBuffer = new StringBuffer();
        TableInfo tableInfo = TableHelperUtil.getTableInfo(clazz);
        sqlBuffer.append("alter table " + tableInfo.getTableName() + " add constraint " + fkInfo.getFkName() + " foreign key (" + fkInfo.getFkColumnName()
                + ") references " + fkInfo.getPkTableName() + "(" + fkInfo.getPkColumnName() + ");");
        return sqlBuffer.toString();
    }

    /**
     * 删除外键
     *
     * @param clazz
     * @param fkInfo
     * @return
     */
    @Override
    public <M extends Entity> String drop(Class<M> clazz, TableFkInfo fkInfo, TableMetaFkInfo metaFkInfo) {
        StringBuffer sqlBuffer = new StringBuffer();
        TableInfo tableInfo = TableHelperUtil.getTableInfo(clazz);
        sqlBuffer.append("alter table " + tableInfo.getTableName() + " drop constraint " + metaFkInfo.getFkName() + ";");
        return sqlBuffer.toString();
    }

    /**
     * 修改外键
     *
     * @param clazz
     * @param fkInfo
     * @return
     */
    @Override
    public <M extends Entity> String modify(Class<M> clazz, TableFkInfo fkInfo, TableMetaFkInfo metaFkInfo) {
        StringBuffer sqlBuffer = new StringBuffer();
        TableInfo tableInfo = TableHelperUtil.getTableInfo(clazz);
        sqlBuffer.append("alter table " + tableInfo.getTableName() + " drop constraint " + metaFkInfo.getFkName() + ";");
        sqlBuffer.append(StringPool.NEWLINE).append("alter table " + tableInfo.getTableName() + " add constraint " + fkInfo.getFkName() + " foreign key (" + fkInfo.getFkColumnName()
                + ") references " + fkInfo.getPkTableName() + "(" + fkInfo.getPkColumnName() + ");");
        return sqlBuffer.toString();
    }

    @Override
    public <M extends Entity> Boolean match(Class<M> clazz, TableFkInfo fkInfo, TableMetaFkInfo metaFkInfo) {
        // 外键列不一样，返回不匹配
        if (!fkInfo.getFkColumnName().equalsIgnoreCase(metaFkInfo.getFkColumnName())) {
            return false;
        }

        if (!fkInfo.getPkTableName().equalsIgnoreCase(metaFkInfo.getPkTableName())) {
            return false;
        }

        if (!fkInfo.getPkColumnName().equalsIgnoreCase(metaFkInfo.getPkColumnName())) {
            return false;
        }

        return true;
    }
}
